import selenium
from selenium.webdriver.common.by import By
import time
from selenium import webdriver
# 反爬
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# 增加无头
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 防止被网站识别
chrome_options.add_experimental_option('excludeSwitches',['enable-automation'])
brower = webdriver.Chrome(chrome_options=chrome_options)


def weibo_spider(context):
    # try:
    url = 'https://s.weibo.com/'
    # brower = webdriver.Chrome()

    brower.get(url)
    brower.implicitly_wait(10)

    button = brower.find_element_by_xpath("//div[@class='search-input']/input")
    button.send_keys(context)
    bu = brower.find_element_by_xpath("//div[@class='searchbox']/button[@class='s-btn-b']")
    bu.click()
    # brower.execute_script("arguments[0].click();",button)
    time.sleep(3)
    node = brower.find_element(By.XPATH, "//*[@id='pl_feed_main'']/div[2]/div[2]/div[2]/div/div[2]/div/div[2]/p[2]")
    # node = brower.find_element(By.XPATH,"//*[@id='pl_feed_main']/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/p[2]")
    print("微博搜索")
    print(node.text)
    # finally:
    #     brower.close()